package condominio

import grails.converters.JSON

class CuotasController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

	def list = {
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		[cuotasInstanceList: Cuotas.list(params), cuotasInstanceTotal: Cuotas.count()]
	}

	def list1 = {
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		
		def cuotasq = Cuotas.findAllByEstado("0")		
		[cuotasInstanceList: cuotasq, cuotasInstanceTotal: cuotasq.size()]
	}	

	def list2 = {
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		def fecha = new Date()		
		
		def cuotasq = Cuotas.findAll("from Cuotas as b where b.estado = '0' and b.fecha_vencimiento < ?",[new Date()])		
		[cuotasInstanceList: cuotasq, cuotasInstanceTotal: cuotasq.size()]
	}	

	
    def create = {
        def cuotasInstance = new Cuotas()
        cuotasInstance.properties = params
        return [cuotasInstance: cuotasInstance]
    }

    def save = {
        def cuotasInstance = new Cuotas(params)
        if (cuotasInstance.save(flush: true)) {
            flash.message = message(code: 'default.created.message', args: [message(code: 'cuotas.label', default: 'Cuotas'), cuotasInstance.id])
            redirect(action: "show", id: cuotasInstance.id)
        }
        else {
            render(view: "create", model: [cuotasInstance: cuotasInstance])
        }
    }

    def show = {
        def cuotasInstance = Cuotas.get(params.id)
        if (!cuotasInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'cuotas.label', default: 'Cuotas'), params.id])
            redirect(action: "list")
        }
        else {
            [cuotasInstance: cuotasInstance]
        }
    }

    def edit = {
        def cuotasInstance = Cuotas.get(params.id)
        if (!cuotasInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'cuotas.label', default: 'Cuotas'), params.id])
            redirect(action: "list")
        }
        else {
            return [cuotasInstance: cuotasInstance]
        }
    }

    def update = {
        def cuotasInstance = Cuotas.get(params.id)
        if (cuotasInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (cuotasInstance.version > version) {
                    
                    cuotasInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'cuotas.label', default: 'Cuotas')] as Object[], "Another user has updated this Cuotas while you were editing")
                    render(view: "edit", model: [cuotasInstance: cuotasInstance])
                    return
                }
            }
            cuotasInstance.properties = params
            if (!cuotasInstance.hasErrors() && cuotasInstance.save(flush: true)) {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'cuotas.label', default: 'Cuotas'), cuotasInstance.id])
                redirect(action: "show", id: cuotasInstance.id)
            }
            else {
                render(view: "edit", model: [cuotasInstance: cuotasInstance])
            }
        }
        else {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'cuotas.label', default: 'Cuotas'), params.id])
            redirect(action: "list")
        }
    }

    def delete = {
        def cuotasInstance = Cuotas.get(params.id)
        if (cuotasInstance) {
            try {
                cuotasInstance.delete(flush: true)
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'cuotas.label', default: 'Cuotas'), params.id])
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'cuotas.label', default: 'Cuotas'), params.id])
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'cuotas.label', default: 'Cuotas'), params.id])
            redirect(action: "list")
        }
    }
	
}
